package week10;

//*******************************************************************
//  MagazineList.java       Author: Lewis/Loftus
//
//  Represents a collection of magazines.
//*******************************************************************

public class DVDList {
    private DVDNode list;

    //----------------------------------------------------------------
    //  Sets up an initially empty list of magazines.
    //----------------------------------------------------------------
    public DVDList() {
        list = null;
    }

    //----------------------------------------------------------------
    //  Creates a new MagazineNode object and adds it to the end of
    //  the linked list.
    //----------------------------------------------------------------
    public void add(DVD mag) {
        DVDNode node = new DVDNode(mag);
        DVDNode current;

        if (list == null)
        {list = node;}
        else {
            current = list;
            while (current.next != null)
            {current = current.next;}
            current.next = node;
        }
    }

    public void insert(int index, DVD mag) {
        DVDNode node = list;
        int i = 0;
        while (node != null && i < index - 2) {
            node = node.next;
            i++;
        }
        DVDNode sert = new DVDNode(mag);
        sert.next = node.next;
        node.next = sert;

        if (index == 1) {
            DVD temp = list.dvd;
            list.dvd = list.next.dvd;
            list.next.dvd = temp;
        }
    }

    public void delete(DVD mag) {
        DVDNode node = list;
        while (node.next.dvd != mag) {
            node = node.next;
        }
        node.next = node.next.next;
    }


    //----------------------------------------------------------------
    //  Returns this list of magazines as a string.
    //----------------------------------------------------------------
    @Override
    public String toString() {
        String result = "";

        DVDNode current = list;

        while (current != null) {
            result += current.dvd + "\n";
            current = current.next;
        }

        return result;
    }

    public void Sort() {
        DVD temp;
        if (list.dvd.compareTo(list.next.dvd) > 0) {
            temp = list.next.dvd;
            list.next.dvd = list.dvd;
            list.dvd = temp;
        }
        DVDNode temp1 = list;
        DVDNode temp2;

        while (temp1.next != null) {
            temp2 = temp1.next;
            while (temp2.next != null) {
                if (temp1.next.dvd.compareTo(temp2.next.dvd) > 0) {
                    temp = temp2.next.dvd;
                    temp2.next.dvd = temp1.next.dvd;
                    temp1.next.dvd = temp;
                }
                temp2 = temp2.next;
            }
            temp1 = temp1.next;
        }

    }

    //*****************************************************************
    //  An inner class that represents a node in the magazine list.
    //  The public variables are accessed by the MagazineList class.
    //*****************************************************************
    private class DVDNode {
        public DVD dvd;
        public DVDNode next;

        //--------------------------------------------------------------
        //  Sets up the node
        //--------------------------------------------------------------
        public DVDNode(DVD mag) {
            dvd = mag;
            next = null;

        }
    }
}